//
//  LocationCustomMenu.m
//  Contractor
//
//  Created by 金鑫 on 16/6/3.
//  Copyright © 2016年 star j. All rights reserved.
//

#import "LocationCustomMenu.h"

#define TopToView 10.0f
#define LeftToView 10.0f
#define CellLineEdgeInsets UIEdgeInsetsMake(0, 10, 0, 10)
#define kScreenWidth        [UIScreen mainScreen].bounds.size.width
#define kScreenHeight        [UIScreen mainScreen].bounds.size.height

@implementation LocationCustomMenu

- (instancetype)initWithDataArr:(NSArray *)dataArr origin:(CGPoint)origin width:(CGFloat)width rowHeight:(CGFloat)rowHeight
{
    self = [super initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    if (self) {
        if (rowHeight <= 0) {
            rowHeight = 44;
        }
        self.backgroundColor = [UIColor clearColor];
        self.origin = origin;
        self.rowHeight = rowHeight;
        self.arrData = [dataArr copy];
        if (dataArr.count > 10) {
            self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(origin.x , origin.y , width, 300) style:UITableViewStylePlain];
        }
        else{
            self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(origin.x , origin.y , width, rowHeight * dataArr.count) style:UITableViewStylePlain];
        }
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [self addSubview:_tableView];
       
        _tableView.backgroundColor = [UIColor whiteColor] ;
        
        //阴影
        _tableView.clipsToBounds = NO ;
        _tableView.layer.shadowColor = [UIColor grayColor].CGColor ;
        _tableView.layer.shadowOpacity = 1.0 ;
        _tableView.layer.shadowRadius = 5.0 ;
        _tableView.layer.shadowOffset = CGSizeMake(0, 3) ;
        
        _tableView.bounces = NO;
        //_tableView.separatorColor = [UIColor colorWithWhite:0.3 alpha:1];
        _tableView.separatorColor = [UIColor clearColor] ;
        
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"LTCustomMenu"];
//        if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
//            
//            [self.tableView setSeparatorInset:CellLineEdgeInsets];
//            
//        }
//        
//        if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
//            
//            [self.tableView setLayoutMargins:CellLineEdgeInsets];
//            
//        }
        
    }
    return self;
    
}

//- (void)layoutSubviews
//{
//    [super layoutSubviews];
//    
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrData.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.rowHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LTCustomMenu"];
    cell.backgroundColor = [UIColor clearColor];
    
    //    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    NSLog(@"indexpath.row=%ld",(long)indexPath.row) ;
    cell.textLabel.textColor = [UIColor blackColor];
    cell.textLabel.textAlignment = NSTextAlignmentLeft ;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = self.arrData[indexPath.row];
    cell.backgroundColor = [UIColor whiteColor] ;
    
    return cell ;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([self.delegate respondsToSelector:@selector(ltCustomMenu:didSelectRowAtIndexPath:)]){
        [self.delegate ltCustomMenu:tableView didSelectRowAtIndexPath:indexPath] ;
        NSLog(@"工作了") ;
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self dismissWithCompletion:nil];
}

//- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
//
//{
//    
//    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
//        
//        [cell setSeparatorInset:CellLineEdgeInsets];
//        
//    }
//    
//    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
//        
//        [cell setLayoutMargins:CellLineEdgeInsets];
//        
//    }
//    
//}

- (void)dismissWithCompletion:(void (^)(LocationCustomMenu *object))completion
{
    __weak __typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.2 animations:^{
//        weakSelf.alpha = 0;
//        weakSelf.tableView.frame = CGRectMake(weakSelf.origin.x + LeftToView, weakSelf.origin.y + TopToView, 0, 0);
    } completion:^(BOOL finished) {
        [weakSelf removeFromSuperview];
        if (completion) {
            completion(weakSelf);
        }
        if (weakSelf.dismiss) {
            weakSelf.dismiss();
        }
    }];
}



@end
